package com.bsoft.gol.hcs.visitrecord.dao;

import com.bsoft.gol.hcs.enumcase.FollowTypeEnum;
import com.bsoft.gol.hcs.his.bean.request.QueryOutpatientListRequest;
import com.bsoft.gol.hcs.his.bean.response.PatientInfo;
import com.bsoft.gol.hcs.specialist.bean.request.GroupChronicRecordQo;
import com.bsoft.gol.hcs.specialist.bean.request.VisitMedicalListRequest;
import com.bsoft.gol.hcs.specialist.bean.response.FactorData;
import com.bsoft.gol.hcs.specialist.bean.response.OutPatientInfoListResponse;
import com.bsoft.gol.hcs.specialist.bean.response.PatientSearchProcess;
import com.bsoft.gol.hcs.specialist.bean.response.VisitMedicalListResponse;
import com.bsoft.gol.hcs.specialist.entity.PatientMedicalDataCollectEntity;
import com.bsoft.gol.hcs.specialist.entity.PatientSearchRuleDetail;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.utils.Constants;
import com.bsoft.gol.hcs.utils.DateUtil;
import com.bsoft.gol.hcs.utils.DateUtils;
import com.bsoft.gol.hcs.visitrecord.bean.qo.HospitalVisitsRecordListQo;
import com.bsoft.gol.hcs.visitrecord.bean.vo.HospitalVisitsRecordListVo;
import com.bsoft.gol.hcs.visitrecord.bean.vo.OutpatientRecordListVo;
import com.google.common.collect.Lists;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.base.VrOutpatientVisitRecord;
import org.apache.commons.lang3.StringUtils;
import org.atag.dao.action.QoHibernateStatelessResultAction;
import org.atag.dao.ext.HibernateExtendDAO;
import org.atag.dao.model.SimplePageInfo;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.transform.Transformers;

import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by ChenJij on 2020/9/4.
 */
public abstract class VrOutpatientVisitRecordDAO extends HibernateExtendDAO<VrOutpatientVisitRecord> {
    public VrOutpatientVisitRecordDAO() {
        this.setKeyField("visitRecordId");
    }


    /**
     * @description: 更新满意度调查已完成数量
     * @author: chenJij@bsoft.com.cn
     * @date: 2020/10/9 13:25
     */
    @DAOMethod(sql = "update VrOutpatientVisitRecord set finishNumber=finishNumber+1 where  visitRecordId=:businessId")
    public abstract void updateFinishNumber(@DAOParam("businessId") String businessId);

    /**
     * 更新是否删除标志
     */
    @DAOMethod(sql = "update VrOutpatientVisitRecord set deleteFlag='1' where visitRecordId in (:recordIdList) ")
    public abstract void updateDeleteFlag(@DAOParam("recordIdList") List<String> recordIdList);

    /**
     * 根据日期删除门诊记录
     *
     * @param resultDate
     */
    @DAOMethod(sql = "delete from VrOutpatientVisitRecord where end_time <:resultDate and delete_flag !='1' ")
    public abstract void deleteOutpatientOverdueData(@DAOParam("resultDate") Date resultDate);

    /**
     * 查询所有的病人id
     *
     * @return java.util.List<java.lang.String>
     * @throws
     * @author tongtp
     * @date 2022/6/14 15:15
     */
    public List<String> queryAllPatientId(final List<String> departmentIds) {
        HibernateStatelessResultAction<List<String>> action =
                new AbstractHibernateStatelessResultAction<List<String>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append("SELECT DISTINCT patient_id AS patientId ");
                        sqlBuilder.append(" FROM vr_outpatient_visit_record ");
                        sqlBuilder.append(" WHERE patient_id is not null ");
                        if (CommonUtils.isNotEmpty(departmentIds)) {
                            sqlBuilder.append(" and department_id in (:departmentIds) ");
                        }
                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        if (CommonUtils.isNotEmpty(departmentIds)) {
                            query.setParameterList("departmentIds", departmentIds);
                        }
                        List list = query.list();
                        List<String> voList;
                        if (CommonUtils.isNotEmpty(list)) {
                            voList = list;
                        } else {
                            voList = new ArrayList<>();
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 查询门诊住院记录
     *
     * @param request 入参
     * @return 门诊住院信息
     */
    public List<VisitMedicalListResponse> queryVisitMedicalListByCard(VisitMedicalListRequest request) {
        HibernateStatelessResultAction<List<VisitMedicalListResponse>> action =
                new AbstractHibernateStatelessResultAction<List<VisitMedicalListResponse>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append("SELECT r.organization_id as organizationId,r" +
                                ".organization_name " +
                                "as organizationName,r.department_id as departmentId,r.department_name as " +
                                "departmentName");
                        if (Constants.ONE_STRING.equals(request.getRecordType())) {
                            sqlBuilder.append(",r.visit_record_id as recordId,r.visit_no as visitId,r" +
                                    ".main_diagnosis_code as " +
                                    "mainDiagnosisCode,r.main_diagnosis_name as mainDiagnosisName,r.start_time as " +
                                    "startTime ");
                            sqlBuilder.append(" from vr_outpatient_visit_record r ");

                            sqlBuilder.append(" where r.card_number=:cardNumber and r.card_type=:cardType " +
                                    " and r.start_time>=:startTime and r.start_time<=:endTime ");
                            sqlBuilder.append(" order by r.start_time desc ");
                        } else {
                            sqlBuilder.append(",r.hospital_record_id as recordId,r.in_hospital_record_code as " +
                                    "visitId,r" +
                                    ".admission_diagnosis as admissionDiagnosis,r.discharge_diagnosis as " +
                                    "dischargeDiagnosis,r" +
                                    ".admission_time as startTime ");
                            sqlBuilder.append(" from vr_inpatient_visit_record r ");

                            sqlBuilder.append(" where r.card_number=:cardNumber and r.card_type=:cardType " +
                                    " and r.admission_time>=:startTime and r.admission_time<=:endTime ");
                            sqlBuilder.append(" order by r.admission_time desc ");
                        }

                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        query.setParameter("cardNumber", request.getCardNumber());
                        query.setParameter("cardType", request.getCardType());
                        query.setParameter("startTime", request.getStartTime());
                        query.setParameter("endTime", request.getEndTime());


                        query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                        List<Map<String, String>> list = query.list();
                        if (CommonUtils.isNotEmpty(list)) {
                            List<VisitMedicalListResponse> collectEntityList = Lists.newArrayList();
                            for (Map map : list) {
                                VisitMedicalListResponse response = ConversionUtils.convert(map,
                                        VisitMedicalListResponse.class);
                                collectEntityList.add(response);
                            }
                            setResult(collectEntityList);
                        }
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 根据病人id查询最后一条住院记录
     * 很多数据的end_time都是空的，所以这里用start_time来排序
     *
     * @param patientId
     * @param departmentId
     * @return
     */
    public VrOutpatientVisitRecord getByPatientIdAndLastOne(final String patientId, final String departmentId) {
        HibernateStatelessResultAction<VrOutpatientVisitRecord> action =
                new AbstractHibernateStatelessResultAction<VrOutpatientVisitRecord>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        List departmentIds = null;
                        if (CommonUtils.isNotNull(departmentId)) {
                            departmentIds = Arrays.asList(departmentId.split(","));
                        }
                        StringBuilder builder = new StringBuilder();
                        builder.append(" from VrOutpatientVisitRecord  where patientId = :patientId ");
                        if (CommonUtils.isNotEmpty(departmentIds)) {
                            builder.append(" and departmentId in (:departmentIds) ");
                        }
                        builder.append(" order by startTime desc");
                        Query query = ss.createQuery(builder.toString());
                        query.setParameter("patientId", patientId);
                        if (CommonUtils.isNotEmpty(departmentIds)) {
                            query.setParameterList("departmentIds", departmentIds);
                        }
//                query = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                        // 查询的条数
                        query.setMaxResults(1);
                        // 开始查询的位置
                        query.setFirstResult(0);
                        List<Map<String, Object>> dbList = query.list();
                        if (CommonUtils.isNotEmpty(dbList)) {
                            setResult(ConversionUtils.convert(dbList.get(0), VrOutpatientVisitRecord.class));
                        } else {
                            setResult(null);
                        }
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    @DAOMethod(sql = " from VrOutpatientVisitRecord  where visitNo =:visitNo and organizationId=:organizationId")
    public abstract VrOutpatientVisitRecord getByVisitNo(@DAOParam("visitNo") String visitNo, @DAOParam(
            "organizationId") String organizationId);

    /**
     * 更新院后随访生成计划次数
     *
     * @param visitNo
     * @param organizationId
     * @return void
     * @throws
     * @author tongtp
     * @date 2022/6/7 19:30
     */
    @DAOMethod(sql = " update VrOutpatientVisitRecord set fusAfterPlanTimes=fusAfterPlanTimes+1   where visitNo =:visitNo " +
            " and organizationId=:organizationId")
    public abstract void updateFusAfterPlanTimes(@DAOParam("visitNo") String visitNo,
                                                 @DAOParam("organizationId") String organizationId);

    /**
     * 查询同步数量
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @DAOMethod(sql = "select count(1) from VrOutpatientVisitRecord  where  startTime>=:startTime and " +
            "startTime<=:endTime")
    public abstract Long getOutpatientByToday(@DAOParam("startTime") Date startTime, @DAOParam("endTime") Date endTime);

    /**
     * 根据门诊号码批量查询
     */
    @DAOMethod(sql = "from VrOutpatientVisitRecord  where  visitNo in(:visitNoSet) ")
    @Deprecated
    public abstract List<VrOutpatientVisitRecord> findByVisitNoSet(@DAOParam("visitNoSet") List<String> visitNoSet);

    @DAOMethod(sql = " from VrOutpatientVisitRecord where visitNo =:visitNo and organizationId=:organizationId")
    public abstract VrOutpatientVisitRecord getVrOutpatientVisitRecord(@DAOParam("visitNo") String visitNo, @DAOParam("organizationId") String organizationId);

    public SimplePageInfo<HospitalVisitsRecordListVo> queryOutpatientVisitRecordList(HospitalVisitsRecordListQo qo) {
        QoHibernateStatelessResultAction<HospitalVisitsRecordListVo, HospitalVisitsRecordListQo> action = new QoHibernateStatelessResultAction(true, qo, HospitalVisitsRecordListVo.class);
        action.setSql(" SELECT {{" +
                " '1' businessType, " +
                " organization_id AS organizationId, " +
                " patient_id AS patientId, " +
                " visit_record_id AS businessId, " +
                " patient_name AS patientName, " +
                " patient_sex AS patientSex, " +
                " patient_age_text AS patientAge, " +
                " department_name AS departmentName, " +
                " survey_number AS surveyNumber, " +
                " finish_number AS finishNumber, " +
                " patient_medical_card_number AS patientMedicalCardNumber, " +
                " patient_medical_card_type AS patientMedicalCardType, " +
                " card_number AS cardNumber, " +
                " card_type AS cardType, " +
                " telephone, " +
                " main_diagnosis_name AS mainDiagnosisName, " +
                " start_time AS clinicTime }}" +
                "FROM " +
                " vr_outpatient_visit_record where 1=1 ");
        action.addCondition(" and patient_name =:patientName ");
        action.addCondition(" and telephone =:telephone ");
        action.addCondition(" and patient_medical_card_number =:patientMedicalCardNumber ");
        action.addCondition(" and organization_id =:organizationId ");
        action.addCondition(" and department_id =:departmentId ");
        action.addCondition(" and start_time >=:beginDate ");
        action.addCondition(" and start_time <:endDate ");
        action.addCondition(" and main_diagnosis_name =:diseasesName ");
        HibernateSessionTemplate.instance().execute(action);
        SimplePageInfo<HospitalVisitsRecordListVo> pageInfo = action.getSimpleResultPage();
        return pageInfo;
    }

    public SimplePageInfo<OutpatientRecordListVo> queryOutpatientRecordVos(HospitalVisitsRecordListQo qo) {
        QoHibernateStatelessResultAction<OutpatientRecordListVo, HospitalVisitsRecordListQo> action = new QoHibernateStatelessResultAction(true, qo, OutpatientRecordListVo.class);
        action.setSql(" SELECT {{" +
                " visit_no AS visitNo, " +
                " registered_seria_no AS registeredSeriaNo, " +
                " patient_id AS patientId, " +
                " patient_name AS patientName, " +
                " patient_sex AS patientSex, " +
                " patient_age_text AS patientAge, " +
                " department_id AS departmentId, " +
                " department_name AS departmentName, " +
                " organization_id AS organizationId, " +
                " organization_name AS organizationName, " +
                " doctor_id AS doctorId, " +
                " doctor_name AS doctorName, " +
                " start_time AS startTime, " +
                " patient_medical_card_number AS patientMedicalCardNumber, " +
                " patient_medical_card_type AS patientMedicalCardType, " +
                " card_number AS cardNumber, " +
                " card_type AS cardType, " +
                " telephone, " +
                " main_diagnosis_name AS mainDiagnosisName }}" +
                " FROM " +
                " vr_outpatient_visit_record where 1=1 ");
        action.addCondition(" and patient_name =:patientName ");
        action.addCondition(" and telephone =:telephone ");
        action.addCondition(" and patient_medical_card_number =:patientMedicalCardNumber ");
        action.addCondition(" and organization_id =:organizationId ");
        action.addCondition(" and department_id =:departmentId ");
        action.addCondition(" and start_time >=:beginDate ");
        action.addCondition(" and start_time <:endDate ");
        action.addCondition(" and main_diagnosis_name =:diseasesName ");
        action.addCondition(" and patient_id =:patientId ");

        action.addSql(" order by  start_time desc");

        HibernateSessionTemplate.instance().execute(action);
        SimplePageInfo<OutpatientRecordListVo> pageInfo = action.getSimpleResultPage();
        return pageInfo;
    }

    /**
     * 分页查询门诊记录
     *
     * @param qo 入参
     * @return 门诊记录
     */
    public SimplePageInfo<PatientInfo> queryLocalOutpatientList(QueryOutpatientListRequest qo) {
        QoHibernateStatelessResultAction<PatientInfo, QueryOutpatientListRequest> action = new QoHibernateStatelessResultAction(false, qo, PatientInfo.class);
        action.setSql(" SELECT {{" +
                " v.visit_no AS admNumber, " +
                " v.patient_id AS patientId, " +
                " v.patient_name AS patientName, " +
                " v.patient_sex AS patientSex, " +
                " v.patient_age AS patientAge, " +
                " v.department_id AS departmentId, " +
                " v.department_name AS departmentName, " +
                " v.doctor_id AS doctorId, " +
                " v.doctor_name AS doctorName, " +
                " v.start_time AS diagnosisDate, " +
                " v.patient_medical_card_number AS patientMedicalCardNumber, " +
                " v.card_number AS cardNumber, " +
                " v.card_type AS cardType, " +
                " v.telephone as patientTelephone, " +
                " v.organization_name as visitOrganizationName, " +
                " v.organization_id as visitOrganization, " +
                " v.main_diagnosis_name AS diagnosisName " +
                " }}" +
                " FROM " +
                " vr_outpatient_visit_record v where 1=1 and  (v.delete_flag !='1' or v.delete_flag is null ) ");

        if (StringUtils.isNotEmpty(qo.getOrganizationId())) {
            action.addCondition(" and v.organization_id >=:organizationId ");
        }
        if (StringUtils.isNotEmpty(qo.getDepartmentId())) {
            action.addCondition(" and v.department_id =:departmentId ");
        }
        if (StringUtils.isNotEmpty(qo.getScheduleDate())) {
            action.addCondition(" and v.start_time >=:scheduleDate ");
        }
        if (StringUtils.isNotEmpty(qo.getScheduleEndDate())) {
            action.addCondition(" and v.start_time <=:scheduleEndDate ");
        }
        if (StringUtils.isNotEmpty(qo.getDoctorId())) {
            action.addCondition(" and v.doctor_id =:doctorId ");
        }
        if (StringUtils.isNotEmpty(qo.getDiagnosisId())) {
            action.addCondition(" and v.main_diagnosis_code =:diagnosisId ");
        }
        if (CommonUtils.isNotNull(qo.getQueryType()) && Constants.ONE == qo.getQueryType()) {
            action.addCondition(" and v.patient_medical_card_number =:query ");
        }
        if (CommonUtils.isNotNull(qo.getQueryType()) && Constants.TWO == qo.getQueryType()) {
            action.addSql(" and v.patient_name like '" + qo.getQuery() + "%' ");
        }
        action.addSql(" order by  v.start_time desc");

        HibernateSessionTemplate.instance().execute(action);
        List<PatientInfo> firstPatientInfo = action.getResultList();
        SimplePageInfo<PatientInfo> pageInfo = new SimplePageInfo<>();
        List<PatientInfo> screenPatientInfoList = new ArrayList<>();
        for (PatientInfo patientInfo : firstPatientInfo) {
            //勾选了隐藏入组患者
            if (qo.isHidePatient()) {
                BigInteger count = this.queryDiseaseNameCount(patientInfo.getCardNumber(), patientInfo.getVisitOrganization(), qo.getDiseaseName());
                //如果该患者已经入组了该病种，则过滤该条记录
                if (count.intValue() > 0) {
                    continue;
                } else {
                    List<Map<String, String>> diseaseNameList = this.queryDiseaseNameByCardNumberAndOrgId(patientInfo.getCardNumber(), patientInfo.getVisitOrganization());
                    if (CommonUtils.isNotEmpty(diseaseNameList)) {
                        String diseaseNames = "";
                        String diseaseIds = "";
                        for (Map diseaseNameMap : diseaseNameList) {
                            diseaseNames = diseaseNames + diseaseNameMap.get("diseaseName") + ",";
                            diseaseIds = diseaseIds + diseaseNameMap.get("chronicDiseaseId") + ",";
                        }
                        if (StringUtils.isNotEmpty(diseaseNames)) {
                            patientInfo.setDiseaseName(diseaseNames.substring(0, diseaseNames.length() - 1));
                        }
                        if (CommonUtils.isNotEmpty(diseaseIds)) {
                            patientInfo.setChronicDiseaseIds(diseaseIds.substring(0, diseaseIds.length() - 1));
                        }
                    }
                    screenPatientInfoList.add(patientInfo);
                }
                pageInfo.setTotal(screenPatientInfoList.size());
            } else {
                List<Map<String, String>> diseaseNameList = this.queryDiseaseNameByCardNumberAndOrgId(patientInfo.getCardNumber(), patientInfo.getVisitOrganization());
                if (CommonUtils.isNotEmpty(diseaseNameList)) {
                    String diseaseNames = "";
                    String diseaseIds = "";
                    for (Map diseaseNameMap : diseaseNameList) {
                        diseaseNames = diseaseNames + diseaseNameMap.get("diseaseName") + ",";
                        diseaseIds = diseaseIds + diseaseNameMap.get("chronicDiseaseId") + ",";
                    }
                    if (StringUtils.isNotEmpty(diseaseNames)) {
                        patientInfo.setDiseaseName(diseaseNames.substring(0, diseaseNames.length() - 1));
                    }
                    if (CommonUtils.isNotEmpty(diseaseIds)) {
                        patientInfo.setChronicDiseaseIds(diseaseIds.substring(0, diseaseIds.length() - 1));
                    }
                }
                screenPatientInfoList.add(patientInfo);
            }
            pageInfo.setTotal(screenPatientInfoList.size());
        }
        //处理分页参数
        int pageNo = qo.getPageNo();
        int pageSize = qo.getPageSize();
        List<PatientInfo> patientInfoList = screenPatientInfoList.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        pageInfo.setList(patientInfoList);


//        SimplePageInfo<PatientInfo> pageInfo = action.getSimpleResultPage();
//        List<PatientInfo> patientInfoList = pageInfo.getList();
//        if (CommonUtils.isEmpty(patientInfoList)) {
//            return pageInfo;
//        }
//        pageInfo.setTotal(pageInfo.getTotal());
//        List<PatientInfo> screenPatientInfoList = new ArrayList<>();
//        for (PatientInfo patientInfo : patientInfoList) {
//            //勾选了隐藏入组患者
//            if (qo.isHidePatient()) {
//                BigInteger count = this.queryDiseaseNameCount(patientInfo.getCardNumber(), patientInfo.getVisitOrganization(), qo.getDiseaseName());
//                //如果该患者已经入组了该病种，则过滤该条记录
//                if (count.intValue() > 0) {
//                    continue;
//                } else {
//                    List<Map<String, String>> diseaseNameList = this.queryDiseaseNameByCardNumberAndOrgId(patientInfo.getCardNumber(), patientInfo.getVisitOrganization());
//                    if (CommonUtils.isNotEmpty(diseaseNameList)) {
//                        String diseaseNames = "";
//                        String diseaseIds = "";
//                        for (Map diseaseNameMap : diseaseNameList) {
//                            diseaseNames = diseaseNames + diseaseNameMap.get("diseaseName") + ",";
//                            diseaseIds = diseaseIds + diseaseNameMap.get("chronicDiseaseId") + ",";
//                        }
//                        if (StringUtils.isNotEmpty(diseaseNames)) {
//                            patientInfo.setDiseaseName(diseaseNames.substring(0, diseaseNames.length() - 1));
//                        }
//                        if (CommonUtils.isNotEmpty(diseaseIds)) {
//                            patientInfo.setChronicDiseaseIds(diseaseIds.substring(0, diseaseIds.length() - 1));
//                        }
//                    }
//                    screenPatientInfoList.add(patientInfo);
//                }
//                //隐藏了患者之后，则需要重新赋值总数
//                pageInfo.setTotal(screenPatientInfoList.size());
//            } else {
//                List<Map<String, String>> diseaseNameList = this.queryDiseaseNameByCardNumberAndOrgId(patientInfo.getCardNumber(), patientInfo.getVisitOrganization());
//                if (CommonUtils.isNotEmpty(diseaseNameList)) {
//                    String diseaseNames = "";
//                    String diseaseIds = "";
//                    for (Map diseaseNameMap : diseaseNameList) {
//                        diseaseNames = diseaseNames + diseaseNameMap.get("diseaseName") + ",";
//                        diseaseIds = diseaseIds + diseaseNameMap.get("chronicDiseaseId") + ",";
//                    }
//                    if (StringUtils.isNotEmpty(diseaseNames)) {
//                        patientInfo.setDiseaseName(diseaseNames.substring(0, diseaseNames.length() - 1));
//                    }
//                    if (CommonUtils.isNotEmpty(diseaseIds)) {
//                        patientInfo.setChronicDiseaseIds(diseaseIds.substring(0, diseaseIds.length() - 1));
//                    }
//                }
//                screenPatientInfoList.add(patientInfo);
//            }
//        }
//        pageInfo.setList(screenPatientInfoList);
        return pageInfo;
    }

    /**
     * 查询慢病名称
     *
     * @param cardNumber     身份证号
     * @param organizationId 机构编码
     * @return 慢病名称
     */
    public List<Map<String, String>> queryDiseaseNameByCardNumberAndOrgId(final String cardNumber, final String organizationId) {
        HibernateStatelessResultAction<List<Map<String, String>>> action =
                new AbstractHibernateStatelessResultAction<List<Map<String, String>>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append("SELECT chronic_disease_id as chronicDiseaseId,chronic_disease_name as diseaseName from fus_team_chronic_group_record " +
                                " WHERE card_number =:cardNumber and organization_id =:organizationId and apply_status = '1'");
                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        query.setParameter("cardNumber", cardNumber);
                        query.setParameter("organizationId", organizationId);
                        query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                        List<Map<String, String>> list = query.list();
                        if (CommonUtils.isNotEmpty(list)) {
                            setResult(list);
                        }
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 查询该患者是否入组了该病种
     *
     * @param cardNumber
     * @param organizationId
     * @return
     */
    public BigInteger queryDiseaseNameCount(final String cardNumber, final String organizationId, final String diseaseName) {
        HibernateStatelessResultAction<BigInteger> action =
                new AbstractHibernateStatelessResultAction<BigInteger>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append("SELECT count(1) as count from fus_team_chronic_group_record " +
                                " WHERE card_number =:cardNumber and organization_id =:organizationId ");
                        if (CommonUtils.isNotEmpty(diseaseName)) {
                            sqlBuilder.append(" and chronic_disease_name =:diseaseName ");
                        }
                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        query.setParameter("cardNumber", cardNumber);
                        query.setParameter("organizationId", organizationId);
                        if (CommonUtils.isNotEmpty(diseaseName)) {
                            query.setParameter("diseaseName", diseaseName);
                        }
                        List list = query.list();
                        if (list.size() == 1) {
                            Object o = list.get(0);
                            BigInteger count = (BigInteger) o;
                            setResult(count);
                        }
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 根据病人id和病种id获取门诊信息
     *
     * @param qo
     * @return
     */
    public List<OutPatientInfoListResponse> queryAnswerOutPatientInfoList(GroupChronicRecordQo qo) {
        HibernateStatelessResultAction<List<OutPatientInfoListResponse>> action = new AbstractHibernateStatelessResultAction<List<OutPatientInfoListResponse>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("SELECT visit_record_id as outPatientInfoId,end_time as visitTime from vr_outpatient_visit_record WHERE card_number =:idCard");
                sqlBuilder.append(" and end_time >=:beginTime and end_time <=:endTime order by end_time ");
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                query.setParameter("idCard", qo.getIdCard());
                if (CommonUtils.isNotEmpty(qo.getBeginTime())){
                    query.setParameter("beginTime", qo.getBeginTime()+" 00:00:00");
                }
                if (CommonUtils.isNotEmpty(qo.getBeginTime())){
                    query.setParameter("endTime", qo.getEndTime()+" 23:59:59");
                }
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                List<OutPatientInfoListResponse> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, OutPatientInfoListResponse.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 查询门诊患者记录表内接诊医生
     *
     * @param organizationId 机构编码
     * @return 医生
     */
    public List<Map<String, String>> outPatientDoctorList(final String organizationId) {
        HibernateStatelessResultAction<List<Map<String, String>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, String>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("SELECT doctor_id as doctorId,doctor_name as doctorName from vr_outpatient_visit_record WHERE organization_id =:organizationId group by doctor_id,doctor_name ");
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                query.setParameter("organizationId", organizationId);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map<String, String>> list = query.list();
                if (CommonUtils.isNotEmpty(list)) {
                    setResult(list);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 查询门诊、住院记录
     *
     * @param departmentIdList 科室集合
     * @param diagnosisList    诊断集合
     * @param indexList        指标集合
     * @param operateList      手术集合
     * @param operativeList    操作集合
     * @param drugList         用药集合
     * @return 门诊、住院记录
     */
    public List<PatientMedicalDataCollectEntity> queryPatientDataList(String collectClass, List<String> departmentIdList, List<String> diagnosisList, List<String> indexList, List<String> operateList, List<String> operativeList, List<String> drugList) {
        HibernateStatelessResultAction<List<PatientMedicalDataCollectEntity>> action = new AbstractHibernateStatelessResultAction<List<PatientMedicalDataCollectEntity>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                Date date = new Date();
                String currentDate = DateUtils.dateFormate(date, "yyyy-MM-dd HH:mm:ss");
                String yearAgo = DateUtils.dateFormate(DateUtils.dateSubYear(date, Constants.ONE), "yyyy-MM-dd HH:mm:ss");

                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("SELECT r.patient_id as patientId,r.patient_name as patientName,r.patient_medical_card_type as patientMedicalCardType" +
                        ",r.patient_medical_card_number as patientMedicalCardNumber,r.card_type as cardType,r.card_number as cardNumber");
                if (Constants.ONE_STRING.equals(collectClass)) {
                    sqlBuilder.append(",r.end_time as recordTime,r.visit_record_id as recordId,1 as recordType from vr_outpatient_visit_record r where r.end_time>=:yearAgo and r.end_time<=:currentDate ");
                } else {
                    sqlBuilder.append(",r.admission_time as recordTime ,r.hospital_record_id as recordId,2 as recordType from vr_inpatient_visit_record r where r.discharge_time>=:yearAgo and r.discharge_time<=:currentDate ");
                }

                if (CommonUtils.isNotEmpty(departmentIdList) || CommonUtils.isNotEmpty(diagnosisList) || CommonUtils.isNotEmpty(indexList) || CommonUtils.isNotEmpty(operateList) || CommonUtils.isNotEmpty(operativeList) || CommonUtils.isNotEmpty(drugList)) {
                    if (Constants.ONE_STRING.equals(collectClass)) {
                        sqlBuilder.append(" and r.visit_record_id in ( ");
                    } else {
                        sqlBuilder.append(" and r.hospital_record_id in ( ");
                    }
                    sqlBuilder.append(" select a.businessId from ((select 0 as businessId ) ");
                    if (CommonUtils.isNotEmpty(departmentIdList)) {
                        if (Constants.ONE_STRING.equals(collectClass)) {
                            sqlBuilder.append(" UNION (select ov.visit_record_id as businessId from vr_outpatient_visit_record ov where 1=1 ");
                        } else {
                            sqlBuilder.append(" UNION (select ov.hospital_record_id as businessId from vr_inpatient_visit_record ov where 1=1 ");
                        }
                        if (departmentIdList.size() > Constants.ONE) {
                            sqlBuilder.append(" and ov.department_id in(:departmentIdList)");
                        }
                        sqlBuilder.append(" )");
                    }
                    if (CommonUtils.isNotEmpty(diagnosisList)) {
                        sqlBuilder.append(" UNION (select d.business_id as businessId from vr_visit_diagnosis d where d.diseases_code in(:diagnosisList)) ");
                    }
                    if (CommonUtils.isNotEmpty(indexList)) {
                        sqlBuilder.append(" UNION (select e.business_id as businessId from exam_report e where e.exam_item_code in(:indexList)) ");
                        sqlBuilder.append(" UNION (select i.business_id as businessId from inspect_report_record i left join inspect_report_routine_detail id " +
                                " on i.inspect_report_record_id = id.inspect_report_id where id.test_id in(:indexList)) ");
                    }
                    if (CommonUtils.isNotEmpty(operativeList)) {
                        sqlBuilder.append(" UNION (select s.business_id as businessId from vr_surgical_record s where s.operation_code in(:operativeList)) ");
                    }
                    if (CommonUtils.isNotEmpty(operateList)) {
                        sqlBuilder.append(" UNION (select t.business_id as businessId from treatment_record t where t.treatment_item_code in(:operateList)) ");
                    }
                    if (CommonUtils.isNotEmpty(drugList)) {
                        sqlBuilder.append(" UNION (select p.business_id as businessId from prescription_record p where p.drug_detail_id in(:drugList)) ");
                    }
                    sqlBuilder.append(" ) a )");
                }
                sqlBuilder.append("  order by r.create_time asc ");
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                if (CommonUtils.isNotEmpty(departmentIdList) && departmentIdList.size() > Constants.ONE) {
                    query.setParameterList("departmentIdList", departmentIdList);
                }
                if (CommonUtils.isNotEmpty(diagnosisList)) {
                    query.setParameterList("diagnosisList", diagnosisList);
                }
                if (CommonUtils.isNotEmpty(indexList)) {
                    query.setParameterList("indexList", indexList);
                }
                if (CommonUtils.isNotEmpty(operativeList)) {
                    query.setParameterList("operativeList", operativeList);
                }
                if (CommonUtils.isNotEmpty(operateList)) {
                    query.setParameterList("operateList", operateList);
                }
                if (CommonUtils.isNotEmpty(drugList)) {
                    query.setParameterList("drugList", drugList);
                }
                query.setParameter("currentDate", currentDate);
                query.setParameter("yearAgo", yearAgo);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map<String, String>> list = query.list();
                if (CommonUtils.isNotEmpty(list)) {
                    List<PatientMedicalDataCollectEntity> collectEntityList = Lists.newArrayList();
                    for (Map map : list) {
                        PatientMedicalDataCollectEntity collectEntity = ConversionUtils.convert(map, PatientMedicalDataCollectEntity.class);
                        ;
                        collectEntityList.add(collectEntity);
                    }
                    setResult(collectEntityList);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    /**
     * @description: 根据诊断编码、诊断时间、病人id集合查询诊断记录集合
     * @param: factorIdList 诊断编码集合
     * @param: diagnosisTime
     * @param: diagnosisServiceType 业务诊断类型：1入院诊断、2出院诊断、3门诊诊断、5其他诊断
     * @param: patientIdList
     * @return: java.util.List<specialist.bean.response.FactorData>
     */
    public List<FactorData> executeFactorForOutpatient(List<String> factorIdList, Date startTime, Date endTime, String diagnosisServiceType, List<String> patientIdList, Byte isLoopExecute, Long routeDetailId, String followType) {
        HibernateStatelessResultAction<List<FactorData>> action = new AbstractHibernateStatelessResultAction<List<FactorData>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("select patientId,organizationId,visitRecordId,doctorId ,doctorName,visitTime,dataSource from (");
                sqlBuilder.append("SELECT patient_id as patientId,organization_id organizationId,visit_record_id visitRecordId, ");
                sqlBuilder.append(" doctor_id doctorId,doctor_name doctorName,start_time visitTime, '1' dataSource ");
                sqlBuilder.append("  FROM `vr_outpatient_visit_record`  ");
                sqlBuilder.append(" WHERE doctor_id in(:doctorIdList)  ");
                if (null != startTime) {
                    sqlBuilder.append("  and  start_time>:startTime ");
                }
                if (null != endTime) {
                    sqlBuilder.append("  and  start_time<:endTime ");
                }
                if (CommonUtils.isNotEmpty(patientIdList)) {
                    sqlBuilder.append(" and patient_id in(:patientIdList)");
                }
                sqlBuilder.append(") AS  m ");
                //循环执行机制 0 不执行 1 执行
                if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
                    sqlBuilder.append(" LEFT JOIN fus_factor_data_record f  ON f.patient_id = m.patientId   AND f.route_detail_id = :routeDetailId AND f.visit_record_id = m.visitRecordId WHERE f.id IS NULL  ");
                }
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                query.setParameterList("doctorIdList", factorIdList);
                if (null != startTime) {
                    query.setParameter("startTime", startTime);
                }
                if (null != endTime) {
                    query.setParameter("endTime", endTime);
                }
                if (CommonUtils.isNotEmpty(patientIdList)) {
                    query.setParameterList("patientIdList", patientIdList);
                }
                if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
                    query.setParameter("routeDetailId", routeDetailId);
                }
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                List<FactorData> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, FactorData.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<FactorData> executeFactorForOutpatientStatus(List<String> deptIdList, Date endTime, List<String> patientIdList, Byte isLoopExecute, Long routeDetailId, String followType) {
        HibernateStatelessResultAction<List<FactorData>> action = new AbstractHibernateStatelessResultAction<List<FactorData>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("select patientId,visitRecordId,dataSource from (");
                sqlBuilder.append("SELECT patient_id patientId, visit_record_id visitRecordId,'1' as dataSource FROM ");
                sqlBuilder.append(" (SELECT patient_id ,IFNULL(MAX(end_time),MAX(start_time) )end_time,visit_record_id FROM `vr_outpatient_visit_record` ");
                sqlBuilder.append(" WHERE 1=1 ");
                if (CommonUtils.isNotEmpty(deptIdList)) {
                    sqlBuilder.append(" and department_id in (:deptIdList) ");
                }
                sqlBuilder.append(" GROUP BY patient_id) a");
                sqlBuilder.append(" WHERE end_time<:endTime");
                if (CommonUtils.isNotEmpty(patientIdList)) {
                    sqlBuilder.append(" and patient_id in(:patientIdList)");
                }
                sqlBuilder.append(") AS  m ");
                //循环执行机制 0 不执行 1 执行
                if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
                    sqlBuilder.append(" LEFT JOIN fus_factor_data_record f  ON f.patient_id = m.patientId   AND f.route_detail_id = :routeDetailId AND f.visit_record_id = m.visitRecordId WHERE f.id IS NULL  ");
                }
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                query.setParameter("endTime", endTime);
                if (CommonUtils.isNotEmpty(deptIdList)) {
                    query.setParameterList("deptIdList", deptIdList);
                }
                if (CommonUtils.isNotEmpty(patientIdList)) {
                    query.setParameterList("patientIdList", patientIdList);
                }
                if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
                    query.setParameter("routeDetailId", routeDetailId);
                }
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                List<FactorData> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, FactorData.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<FactorData> executeFactorForOutpatientStatus(List<String> deptIdList, Date startTime, Date endTime, List<String> patientIdList, Byte isLoopExecute, Long routeDetailId, String followType) {
        HibernateStatelessResultAction<List<FactorData>> action = new AbstractHibernateStatelessResultAction<List<FactorData>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("select patientId,visitRecordId,dataSource from (");
                sqlBuilder.append("SELECT patient_id patientId,visit_record_id visitRecordId,'1' as dataSource FROM `vr_outpatient_visit_record`");
                sqlBuilder.append(" WHERE 1=1 ");
                if (CommonUtils.isNotEmpty(deptIdList)) {
                    sqlBuilder.append(" and department_id in (:deptIdList) ");
                }
                if (CommonUtils.isNotEmpty(startTime)) {
                    sqlBuilder.append(" and end_time>:startTime");
                }
                if (CommonUtils.isNotEmpty(endTime)) {
                    sqlBuilder.append(" and end_time<:endTime");
                }
                if (CommonUtils.isNotEmpty(patientIdList)) {
                    sqlBuilder.append(" and patient_id in(:patientIdList)");
                }
                sqlBuilder.append(") AS  m ");
                //循环执行机制 0 不执行 1 执行
                if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
                    sqlBuilder.append(" LEFT JOIN fus_factor_data_record f  ON f.patient_id = m.patientId   AND f.route_detail_id = :routeDetailId AND f.visit_record_id = m.visitRecordId WHERE f.id IS NULL  ");
                }
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                if (CommonUtils.isNotEmpty(startTime)) {
                    query.setParameter("startTime", startTime);
                }
                if (CommonUtils.isNotEmpty(endTime)) {
                    query.setParameter("endTime", endTime);
                }
                if (CommonUtils.isNotEmpty(deptIdList)) {
                    query.setParameterList("deptIdList", deptIdList);
                }
                if (CommonUtils.isNotEmpty(patientIdList)) {
                    query.setParameterList("patientIdList", patientIdList);
                }
                if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
                    query.setParameter("routeDetailId", routeDetailId);
                }
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                List<FactorData> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, FactorData.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    public Integer executeFactorForOutpatientStatus(String patientId, Date startTime) {
        HibernateStatelessResultAction<Integer> action =
                new AbstractHibernateStatelessResultAction<Integer>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append("SELECT  (CASE WHEN a.count1 > 0 THEN 1 ELSE ");
                        sqlBuilder.append(" (SELECT COUNT(1)  FROM  fus_answer_outpatient_info  WHERE patient_id = :patientId   AND visit_time > :startTime)  END)  as COUNT ");
                        sqlBuilder.append(" FROM (SELECT   COUNT(1) count1 FROM vr_outpatient_visit_record WHERE patient_Id = :patientId  AND end_time > :startTime) a  ");
                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        query.setParameter("patientId", patientId);
                        query.setParameter("startTime", startTime);
                        List list = query.list();
                        if (list.size() == 1) {
                            Object o = list.get(0);
                            Integer count = (Integer) o;
                            setResult(count);
                        }
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 门诊诊断高级检索
     * @param patientSearchRuleDetail
     * @param recordStartTime
     * @param recordEndTime
     * @return
     */
    public List<PatientSearchProcess> queryOutpatientDiagnosisRecord(final PatientSearchRuleDetail patientSearchRuleDetail, final Date recordStartTime, final Date recordEndTime) {
        HibernateStatelessResultAction<List<PatientSearchProcess>> action =
                new AbstractHibernateStatelessResultAction<List<PatientSearchProcess>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append("SELECT " +
                                "vr.patient_id as patientId, " +
                                "vr.patient_name AS patientName, " +
                                "vr.organization_id as organizationId, " +
                                "vr.organization_name as organizationName, " +
                                "vr.patient_sex AS patientSex, " +
                                "vr.card_type AS cardType, " +
                                "vr.card_number AS cardNumber, " +
                                "vr.patient_age as patientAge, " +
                                "vr.patient_medical_card_number AS patientNumber," +
                                "( SELECT GROUP_CONCAT( distinct f.chronic_disease_id ) FROM fus_team_chronic_group_record f WHERE vr.card_number = f.card_number and f.apply_status = '1') AS chronicDiseaseIds,"+
                                "( SELECT GROUP_CONCAT( distinct f.chronic_disease_name ) FROM fus_team_chronic_group_record f WHERE vr.card_number = f.card_number and f.apply_status = '1') AS chronicDiseaseName " +
                                "FROM " +
                                "vr_outpatient_visit_record vr where 1=1 and (vr.delete_flag !='1' or vr.delete_flag is null ) ");
                        sqlBuilder.append(" and vr.main_diagnosis_name in (:diagnosisList) ");
                        if (CommonUtils.isNotEmpty(recordStartTime) && CommonUtils.isNotEmpty(recordEndTime)) {
                            sqlBuilder.append(" and vr.start_time >=:recordStartTime and vr.start_time <=:recordEndTime");
                        }
                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        query.setParameterList("diagnosisList", patientSearchRuleDetail.getDiagnosisList());
                        if (CommonUtils.isNotEmpty(recordStartTime) && CommonUtils.isNotEmpty(recordEndTime)) {
                            query.setParameter("recordStartTime", DateUtil.dateFormate(recordStartTime,"yyyy-MM-dd"));
                            query.setParameter("recordEndTime", DateUtil.dateFormate(recordEndTime,"yyyy-MM-dd")+" 23:59:59");
                        }
                        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                        List<Map> list = query.list();
                        List<PatientSearchProcess> voList = new ArrayList<>();
                        if (CommonUtils.isNotEmpty(list)) {
                            for (Map map : list) {
                                voList.add(ConversionUtils.convert(map, PatientSearchProcess.class));
                            }
                        } else {
                            voList = new ArrayList<>();
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}